package com.driver.manage.controller;

import com.alibaba.fastjson2.JSON;
import com.driver.manage.domain.GeoVo;
import com.driver.manage.utils.GeoHashUtil;
import com.shenma2009.domain.ResultResponse;
import com.shenma2009.utils.HbaseUtils;
import com.shenma2009.utils.Result;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
* @description: TODO
* @author Administrator
* @date 2023/6/25 0025 14:08
* @version 1.0
*/
@RestController
@RequestMapping("/geo")
public class GeoController {
    @Resource
    private HbaseUtils hbaseUtils;
    @Resource
   private GeoHashUtil geoHashUtil;



    @PostMapping("/createNameSpace/{namespace}")
    public boolean createNameSpace(@PathVariable String namespace) throws IOException {
        boolean nameSpace = hbaseUtils.createNameSpace(namespace);
        return nameSpace;
    }


    //添加节点及位置信息
    @PostMapping("/addGeo")
    public ResultResponse addGeo(@RequestBody GeoVo geoVo){
        geoVo.setGeoKey("天津");
        geoVo.setPointName(geoVo.getPointName());
        geoVo.setLatitude(geoVo.getLatitude());
        geoVo.setLongitude(geoVo.getLongitude());
        geoHashUtil.geoAdd(geoVo);
        System.out.println("=================添加成功=========================");
        return ResultResponse.SUCCESS();
    }



    /**
    * @description: TODO
    * @author 苏鹏锋
     * 以给定的经纬度为中心， 返回键包含的位置元素当中
    * @date 2023/6/26 0026 11:47
    * @version 1.0
    */
    @PostMapping("/findRadius")
    public ResultResponse findRadius(@RequestBody GeoVo geoVo){
        geoVo.setGeoKey("天津");
        geoVo.setLatitude(geoVo.getLatitude());
        geoVo.setLongitude(geoVo.getLongitude());
        geoVo.setMetricUnit(Metrics.KILOMETERS);
        geoVo.setRadius(geoVo.getRadius());
        geoVo.setLimit(geoVo.getLimit());
        List<GeoResult<RedisGeoCommands.GeoLocation<String>>> radius1 = geoHashUtil.findRadius(geoVo);
        String stringJSON = JSON.toJSONString(radius1);
        System.out.println("查询到的是"+stringJSON);
        return  new ResultResponse(200, "搜索成功",stringJSON);
    }
    /**
    * @description: TODO
    * @author 苏鹏锋
     * 计算指定key下两个成员点之间的距离
    * @date 2023/6/26 0026 11:52
    * @version 1.0
    */
    @PostMapping("/calDistance")
    public String calDistance(String geoKey, String member1, String member2, RedisGeoCommands.DistanceUnit unit){
        Distance distance = geoHashUtil.calDistance(geoKey, member1, member2, unit);
        return "查询成功"+distance;
    }


    /**
    * @description: TODO
    * @author 苏鹏锋
     * 根据成员点名称查询位置信息
    * @date 2023/6/26 0026 14:17
    * @version 1.0
    */
    @PostMapping("/geoPosition")
    public String geoPosition(String geoKey, String[] members){
        List<Point> points = geoHashUtil.geoPosition(geoKey, members);
        return "查询成功"+points;
    }

    /**
    * @description: TODO
    * @author 苏鹏锋
     * 以给定的城市为中心， 返回键包含的位置元素当中， 与中心的距离不超过给定最大距离的所有位置元素，并给出所有位置元素与中心的平均距离。
    * @date 2023/6/26 0026 14:20
    * @version 1.0
    */
    @PostMapping("/geoNearByPlace")
    public String geoNearByPlace(String key, String name, Integer distance, Integer count){
        GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults = geoHashUtil.geoNearByPlace(key, name, distance, count);
        return "查询成功"+geoResults;
    }

    /**
    * @description: TODO
    * @author 苏鹏锋
     *返回一个或多个位置元素的 Geohash 表示
    * @date 2023/6/26 0026 14:22
    * @version 1.0
    */
    @PostMapping("/geoHash")
    public String geoHash(String key, String[] members){
        List<String> strings = geoHashUtil.geoHash(key, members);
        return "查询成功"+strings;
    }
}
